/*
 * hybrid_ftl.h
 *
 *  Created on: 22.01.2014
 *      Author: Sascha
 */

#ifndef HYBRID_FTL_H_
#define HYBRID_FTL_H_

// TO EDIT
#define SECTOR_COUNT		64
#define PAGES_PER_SECTOR	256
#define BYTES_PER_PAGE		256
#define ERASE_COUNT_THRESHOLD	10
#define CACHE_SIZE			5

/**
 *  This data type to describe the possible status values returned
 *  by the App interface functions.
 */
typedef enum FTL001_StatusType {
	FTL001_NO_ERROR = 0,
	FTL001_HANDLER_ERROR,
	FTL001_CFI_QUERYID_ERROR,
	FTL001_WRITE_ENABLE_ERROR,
	FTL001_ERASE_SECTOR_ERROR,
	FTL001_SECTOR_INVALID_NOTE,
	FTL001_LOGICAL_ADDRESS_ERROR,
	FTL001_PHYSICAL_ADDRESS_ERROR,
	FTL001_NO_FREE_SECTOR,
	FTL001_GET_STATUS_ERROR
} FTL001_StatusType;

typedef struct FTL001_CFIInfoType {
  /** Primary OEM Command Set */
  uint16_t P_ID;
  /** Address for Primary Extended Table */
  uint16_t P_ADR;
  /** Alternate OEM Command Set */
  uint16_t A_ID;
  /** Address for Alternate OEM Extended Table */
  uint16_t A_ADR;
  /** VCC Min (erase/program) */
  uint8_t VccMin;
  /** VCC Max  (erase/program) */
  uint8_t VccMax;
  /** Vpp Max  (erase/program) */
  uint8_t VppMin;
  /** Vpp Max  (erase/program) */
  uint8_t VppMax;
  /** Typical time-out per single double word program */
  uint8_t WordWriteTimeoutTyp;
  /** Typical time-out for Min. size buffer program */
  uint8_t BufWriteTimeoutTyp;
  /** Typical time-out per individual block erase */
  uint8_t BlockEraseTimeoutTyp;
  /** Typical time-out for full chip erase */
  uint8_t ChipEraseTimeoutTyp;
  /** Max. time-out for double word program */
  uint8_t WordWriteTimeoutMax;
  /** Max. time-out for buffer program */
  uint8_t BufWriteTimeoutMax;
  /** Max. time-out per individual block erase */
  uint8_t BlockEraseTimeoutMax;
  /** Max. time-out for full chip erase */
  uint8_t ChipEraseTimeoutMax;
  /** Device Size */
  uint8_t DevSize;
  /** Flash Device Interface Description */
  uint16_t InterfaceDesc;
  /** Max. number of byte in multi-byte write */
  uint16_t MaxBufWriteSize;
  /** Number of Erase Block Regions within device */
  uint8_t NumEraseRegions;
  /** Erase Block Region x Information */
  uint32_t EraseRegionInfo[4];
} FTL001_CFIInfoType;

/**
 *  FTL001 Stub callback functions
 */
typedef struct FTL001_Handler {
	/** Read ID implementation */
	FTL001_StatusType (*ReadID)(uint16_t  *pSPIReceiveData);
	/** Write Enable implementation */
	FTL001_StatusType (*WriteEnable)(void);
	/** Read Status implementation */
	uint16_t (*ReadStatus)(void);
	/** Erase Sector implementation */
	FTL001_StatusType (*SectorErase)(uint32_t Address);
	/** Program Page implementation */
	FTL001_StatusType (*ProgramPage)(uint32_t Address,uint8_t* pBuf);
	/** Read Page implementation */
	FTL001_StatusType (*ReadPage)(uint32_t Address,uint8_t* pBuf);
	/** Standard configuration implementation */
	FTL001_StatusType (*ConfigureStandard)(void);
} FTL001_Handler;

/**
 * This data structure is the App Handle.
 */
typedef struct FTL001_HandleType {
  /** Handler structure which stores the function pointers. */
  FTL001_Handler* Handler;
} FTL001_HandleType;

/**
 * struct for status of Fee-system
 */
typedef struct fee_status_struct {
	uint32_t min_erase_count;
	uint32_t max_erase_count;
	uint16_t free_sectors_count;
} FEE_StatusType;

FTL001_HandleType FTL001_Handle0;

FTL001_StatusType FTL001_RegisterHandler(FTL001_HandleType*  Handle, FTL001_Handler* Handler);
FTL001_StatusType FTL001_GetCFIInfo(const FTL001_HandleType* Handle, FTL001_CFIInfoType* CFIInfo);
FTL001_StatusType Fee_init();
FTL001_StatusType Fee_write(uint32_t address, uint16_t *Data, uint32_t length);
FTL001_StatusType Fee_read(uint32_t address, uint16_t *Data, uint32_t length);
FTL001_StatusType Fee_format_device();
FTL001_StatusType Fee_get_status(FEE_StatusType *fee_status);

#endif /* HYBRID_FTL_H_ */
